<?php

namespace app\admin\command;

use app\common\library\User;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Exception;
use think\Log;
use think\cache\driver\Redis;

class UpgradeExpert extends Command
{

    protected $_sleep = 30;
    // 休眠间隔
    protected $_doing = false;
    // 是否有数据正在处理
    protected $_tasks = [];


    protected function configure()
    {

        $this->setName('UpgradeExpert')->setDescription('升级达人');
    }

    /**
     * 休眠
     *
     * @param string $second            
     */
    public function sleep($second = "")
    {
        $second = $second ? $second : $this->_sleep;
        sleep(sleep($second)); // TP5的命令行 sleep($second) 不生效
        echo "sleep {$second} seconds ok!time:" . date('Y-m-d H:i:s') . PHP_EOL;
    }


    protected function execute(Input $input, Output $output)
    {

        while (true && ! $this->_doing) {
            $this->_doing = true;
            $this->autoRun();
            $this->_doing = false;
        }
    }

    /**
     * 自动执行
     *
     * @return bool
     */
    protected function autoRun()
    {
        $this->getWorks();
        if ($this->_tasks) {
            foreach ($this->_tasks as $user) {
                echo "start run user:" . $user["mobile"] . PHP_EOL;
                $this->runTask($user);
                echo "end run user:" . $user["mobile"] . PHP_EOL;
            }
        } else {
            $this->sleep(3);
        }
    }


    /**
     * 执行单个用户升级任务
     */
    protected function runTask($user)
    {
        try {
            //处理用户的级别关系和占位
            $row = User::upgradeExpert($user["id"], $user["expert_level"]);
            if ($row) {
                //插入升级记录
                $history = [
                    "is_system" => $user["is_system_hand_upgrade"],
                    "admin_id" => $user["system_hand_upgrade_admin_id"],
                    "user_id" => $user["id"],
                    "before_level" => $user["before_expert_level"],
                    "after_level" => $user["upgradeing_expert_level"],
                    "note" => "定时器升级",
                    "addtime" => time()
                ];
                db("expert_upgrade_history")->insert($history);

                //更新状态
                db("user")->where("id", $user["id"])->update([
                    "is_system_hand_upgrade" => 0,
                    "system_hand_upgrade_admin_id" => 0,
                    "before_expert_level" => 0,
                    "upgradeing_expert_level" => 0,
                    "is_upgraded_expert_level" => 0,
                    "expert_level" => $user["upgradeing_expert_level"],
                ]);
            }else{
                echo '升级失败！';
                var_dump($user);
                //更新状态
                db("user")->where("id", $user["id"])->update([
                    "upgraded_expert_level_time" => (time()+600)
                ]);
            }

        } catch (Exception $e) {
            Log::write($e->getMessage().'99');
        }

//        try {
//            //插入升级记录
//            $history = [
//                "is_system" => $user["is_system_hand_upgrade"],
//                "admin_id" => $user["system_hand_upgrade_admin_id"],
//                "user_id" => $user["id"],
//                "before_level" => $user["before_expert_level"],
//                "after_level" => $user["upgradeing_expert_level"],
//                "note" => "定时器升级",
//                "addtime" => time()
//            ];
//            db("expert_upgrade_history")->insert($history);
//
//            //更新状态
//            db("user")->where("id", $user["id"])->update([
//                "is_system_hand_upgrade" => 0,
//                "system_hand_upgrade_admin_id" => 0,
//                "before_expert_level" => 0,
//                "upgradeing_expert_level" => 0,
//                "is_upgraded_expert_level" => 0,
//                "expert_level" => $user["upgradeing_expert_level"],
//            ]);
//        } catch (Exception $e) {
//            Log::write($e->getMessage());
//        }
    }


    /**
     * 获取升级任务
     */
    protected function getWorks()
    {
        try {
            $user_list = db("user")->where("upgradeing_expert_level", ">", 0)->where("upgraded_expert_level_time", "<", time())->where("is_upgraded_expert_level", 1)->order("upgraded_expert_level_time", "asc")->limit(1)->select();
            // exit(print_r($user_list ));
            return $this->_tasks = $user_list ? $user_list : [];
        } catch (Exception $e) {
            Log::write($e->getMessage());
        }
    }
}
